Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDEState: unconditionally close stream #635

Merged
merged 1 commit into from
Jul 10, 2023
Merged

Conversation

jukzi
Copy link
Contributor

@jukzi jukzi commented Jun 23, 2023

For eager return on manifestStream == null jarFile was not closed.

@github-actions
Copy link

github-actions bot commented Jun 23, 2023

Test Results

     246 files  +       6       246 suites  +6   1h 11m 1s ⏱️ + 30m 40s
  3 287 tests  -        1    3 262 ✔️ ±       0  24 💤 ±  0  0  - 1  1 🔥 ±0 
10 158 runs  +2 721  10 085 ✔️ +2 698  72 💤 +24  0  - 1  1 🔥 ±0 

For more details on these errors, see this check.

Results for commit 617669a. ± Comparison against base commit 7dd409f.

This pull request removes 1 test.
AllPDEMinimalTests org.eclipse.pde.ui.tests.launcher.AllLauncherTests org.eclipse.pde.ui.tests.launcher.FeatureBasedLaunchTest ‑ Unknown test

♻️ This comment has been updated with latest results.

Copy link
Member

@HannesWell HannesWell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general.
A few suggestion for further improvements below.

In a follow up, one could get rid of Hashtable/Dictionary here. It is far too often used in PDE in places were it is not necessary.

try (ZipFile jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ)) {
ZipEntry manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME);
if (manifestEntry != null) {
try (InputStream manifestStream = jarFile.getInputStream(manifestEntry)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream".
So I think we can skip the nested try in this case. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably could, but it's easier to read (also for static analysis tools) to just close any stream instead of looking into javadoc.

@@ -317,45 +317,32 @@ private String getQualifierPropery(String bundleLocation) {

//Return a dictionary representing a manifest. The data may result from plugin.xml conversion
private Dictionary<String, String> basicLoadManifest(File bundleLocation) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would simplify the method if the processing of the input stream would be extraced into a separat method and then called from both locations.
Together with the other two suggestions the code would then be:

	private Dictionary<String, String> basicLoadManifest(File bundleLocation) {
		try {
			if ("jar".equalsIgnoreCase(IPath.fromOSString(bundleLocation.getName()).getFileExtension()) && bundleLocation.isFile()) { //$NON-NLS-1$
				try (ZipFile jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ)) {
					ZipEntry manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME);
					if (manifestEntry != null) {
						return readManifestEntries(jarFile.getInputStream(manifestEntry));
					}
				}
			} else {
				try (InputStream manifestStream = new FileInputStream(new File(bundleLocation, JarFile.MANIFEST_NAME))) {
					return readManifestEntries(manifestStream);
				}
			}
		} catch (IOException | BundleException e) {
			//ignore
		}

		//It is not a manifest, but a plugin or a fragment
		return null;
	}

	private Dictionary<String, String> readManifestEntries(InputStream manifestStream) throws IOException, BundleException {
		Hashtable<String, String> result = new Hashtable<>();
		result.putAll(ManifestElement.parseBundleManifest(manifestStream, null));
		return result;
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overhead of introducing an additional method is in this case bigger then the code reduction.

For eager return on manifestStream == null jarFile was not closed.
@jukzi
Copy link
Contributor Author

jukzi commented Jul 10, 2023

ignoring random fail PlainJavaProjectTest #555

@jukzi jukzi merged commit 7736600 into eclipse-pde:master Jul 10, 2023
@jukzi jukzi deleted the PDEState branch July 10, 2023 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants